﻿/*
 * Copyright (c) 2011 Shawn Rakowski
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to 
 * deal in the Software without restriction, including without limitation the 
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 
 * sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
 * SOFTWARE. 
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using ROS.Persistence.DAO.DTO;

namespace ROS.Persistence.DAO.MySQLDAO
{
    internal class MenuItemDAO : MySQLDAO, IMenuItemDAO
    {
        #region Fields

        private MySQLDAOCmdExecuter<MenuItemDTO> cmdExec;

        #endregion

        #region Contructor

        public MenuItemDAO()
        {
            cmdExec = new MySQLDAOCmdExecuter<MenuItemDTO>(this);
        }

        #endregion

        #region Methods

        private const string insertSQL =
            "INSERT INTO menu_item_tbl (is_active, name, is_backend_fulfilled, menu_item_pricing_id) " +
            "VALUES (@is_active, @name, @is_backend_fulfilled, @menu_item_pricing_id)";
        public void Insert(MenuItemDTO menuItemDTO)
        {
            cmdExec.PerformNonQuery(insertSQL, loadParametersFromDTO(menuItemDTO));
            menuItemDTO.ID = cmdExec.GetLastID();
        }

        private const string selectByIDSQL =
            "SELECT * FROM menu_item_tbl WHERE menu_item_id = @menu_item_id";
        public MenuItemDTO SelectByID(long id)
        {
            Dictionary<String, object> parameters = new Dictionary<string, object>();
            parameters["@menu_item_id"] = id;

            MySQLDAOCmdExecuter<MenuItemDTO>.LoadDTODelegate loadMenuItemDTO = 
                new MySQLDAOCmdExecuter<MenuItemDTO>.LoadDTODelegate(loadDTOFromReader);
            MenuItemDTO menuItemDTO = 
                (MenuItemDTO)cmdExec.PerformSingletonSelectQuery(selectByIDSQL, parameters, loadMenuItemDTO);            

            return menuItemDTO;
        }

        const string selectAllSQL = 
            "SELECT * FROM menu_item_tbl";
        public IList<MenuItemDTO> SelectAll()
        {
            MySQLDAOCmdExecuter<MenuItemDTO>.LoadDTODelegate loadMenuItemDTO = 
                new MySQLDAOCmdExecuter<MenuItemDTO>.LoadDTODelegate(loadDTOFromReader);
            return (IList<MenuItemDTO>)cmdExec.PerformSelectQuery(selectAllSQL, null, loadMenuItemDTO);       
        }
      
        private const string updateSQL = 
            "UPDATE menu_item_tbl SET is_active = @is_active, name = @name, " +
            "is_backed_fulfilled = @is_backed_fulfilled" + 
            "WHERE menu_item_id = @menu_item_id";
        public void Update(MenuItemDTO MenuItemDTO)
        {
            cmdExec.PerformNonQuery(updateSQL, loadParametersFromDTO(MenuItemDTO));
        }

        const string deleteByIDSQL = 
            "DELETE FROM menu_item_tbl WHERE menu_item_id = @menu_item_id";
        public void DeleteByID(long id)
        {
            Dictionary<String, object> parameters = new Dictionary<string, object>();
            parameters["@menu_item_id"] = id;
            cmdExec.PerformNonQuery(deleteByIDSQL, parameters);
        }

        private Dictionary<String, object> loadParametersFromDTO(MenuItemDTO dto)
        {
            Dictionary<String, object> parameters = new Dictionary<string, object>();
            parameters["@menu_item_id"] = dto.ID;
            parameters["@is_active"] = dto.IsActive;
            parameters["@name"] = dto.Name;
            parameters["@is_backend_fulfilled"] = dto.IsBackendFulfilled;
            parameters["@menu_item_pricing_id"] = dto.MenuItemPricingID;
            return parameters;
        }

        private MenuItemDTO loadDTOFromReader(MySqlDataReader dataReader)
        {
            MenuItemDTO dto = new MenuItemDTO();
            dto.ID = dataReader.GetInt64("menu_item_id");
            dto.IsActive = dataReader.GetBoolean("is_active");
            dto.Name = dataReader.GetString("name");            
            dto.IsBackendFulfilled = dataReader.GetBoolean("is_backend_fulfilled");
            dto.MenuItemPricingID = dataReader.GetInt64("menu_item_pricing_id");
            return dto;
        }

        #endregion
    }
}
